import { http } from "@/utils/http";

/**
 * 教学工作量查询命令
 * 对应 Java: TeachingWorkloadQuery.java
 */
export interface TeachingWorkloadListCommand extends BasePageQuery {
  userId?: number; //
  deptId?: number; //
  assessmentYear?: number; //
  generalCourseScore?: number; //
  subjectCourseScore?: number; //
  labCourseScore?: number; //
}

/**
 * 教学工作量分页响应
 * 对应 Java: TeachingWorkloadDTO.java
 */
export interface TeachingWorkloadPageResponse {
  id: number; //
  userId: number; //
  assessmentYear: number; //
  generalCourseScore: number; //
  subjectCourseScore: number; //
  labCourseScore: number; //
  totalTeachingScore: number; //
  importedAt: string; //
  importedByUserId: number; //
  createTime: string; // (Java Date 对应 string)
  userName: string; //
  deptName: string; //
}

/**
 * 获取教学工作量列表
 * 对应 Java: TeachingWorkloadController.java -> list()
 */
export function getTeachingWorkloadListApi(params: TeachingWorkloadListCommand) {
  return http.request<ResponseData<PageDTO<TeachingWorkloadPageResponse>>>(
    "get",
    "/performance/teaching/list", //
    {
      params
    }
  );
}

/**
 * 导出教学工作量Excel
 * 对应 Java: TeachingWorkloadController.java -> export()
 */
export const exportTeachingWorkloadExcelApi = (
  params: TeachingWorkloadListCommand,
  fileName: string
) => {
  return http.download("/performance/teaching/excel", fileName, { //
    params
  });
};

/**
 * 获取教学工作量详情
 * 对应 Java: TeachingWorkloadController.java -> getInfo()
 */
export function getTeachingWorkloadInfoApi(id: number) {
  return http.request<ResponseData<TeachingWorkloadPageResponse>>(
    "get",
    `/performance/teaching/${id}` //
  );
}

/**
 * 删除教学工作量
 * 对应 Java: TeachingWorkloadController.java -> delete()
 * 注意: Java控制器使用了 @RequestBody List<Long> ids,
 * 所以我们使用 'data' 而不是 'params'。
 */
export const deleteTeachingWorkloadApi = (data: Array<number>) => {
  return http.request<ResponseData<void>>("delete", "/performance/teaching", { //
    data // 使用 data 来发送JSON数组 [1, 2, 3]
  });
};

/**
 * 新增教学工作量命令
 * 对应 Java: AddTeachingWorkloadCommand.java
 */
export interface AddTeachingWorkloadCommand {
  userId: number; //
  assessmentYear: number; //
  generalCourseScore?: number; //
  subjectCourseScore?: number; //
  labCourseScore?: number; //
  totalTeachingScore?: number; //
  importedByUserId?: number; //
}

/**
 * 新增教学工作量
 * 对应 Java: TeachingWorkloadController.java -> add()
 */
export const addTeachingWorkloadApi = (data: AddTeachingWorkloadCommand) => {
  return http.request<ResponseData<void>>("post", "/performance/teaching", { //
    data
  });
};

/**
 * 更新教学工作量命令
 * 对应 Java: UpdateTeachingWorkloadCommand.java
 */
export interface UpdateTeachingWorkloadCommand extends AddTeachingWorkloadCommand {
  id: number; //
}

/**
 * 修改教学工作量
 * 对应 Java: TeachingWorkloadController.java -> edit()
 */
export const updateTeachingWorkloadApi = (data: UpdateTeachingWorkloadCommand) => {
  return http.request<ResponseData<void>>("put", "/performance/teaching", { //
    data
  });
};

/**
 * 教学工作量导入失败详情
 */
export interface ImportFailureDetail {
  /**
   * 行号（从1开始）
   */
  rowNumber: number;
  /**
   * 用户ID
   */
  userId: number;
  /**
   * 用户姓名
   */
  nickname: string;
  /**
   * 失败原因
   */
  failureReason: string;
  /**
   * 失败数据
   */
  data: any;
}

/**
 * 教学工作量导入结果
 */
export interface TeachingWorkloadImportResultDTO {
  /**
   * 成功导入数量
   */
  successCount: number;
  /**
   * 失败数量
   */
  failCount: number;
  /**
   * 失败详情列表
   */
  failureDetails: ImportFailureDetail[];
}

/**
 * 下载教学工作量导入模板
 */
export function downloadTeachingWorkloadTemplateApi() {
  return http.download(
    "/performance/teaching/excelTemplate",
    `教学工作量导入模板_${new Date().getTime()}.xlsx`
  );
}

/**
 * 导入教学工作量
 * @param file Excel文件
 */
export function importTeachingWorkloadApi(file: File) {
  const formData = new FormData();
  formData.append("file", file);

  return http.request<ResponseData<TeachingWorkloadImportResultDTO>>(
    "post",
    "/performance/teaching/excel",
    {
      data: formData,
      headers: {
        "Content-Type": "multipart/form-data"
      }
    }
  );
}
